﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using application1.Tools;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using application1.ThreadManage;
using System.IO;
using System.IO.Ports;
using application1.User;



namespace application1
{
    class Program
    {
        public static TcpListener MyListener;
        public static List<UserClass> UserList = new List<UserClass>();
        static void Main(string[] args)
        {
            byte[] buffer = new byte[] {  0x33, 0x33, 0xA7, 0x41, };
            string aa = BitConverter.ToSingle(buffer, 0).ToString("f2");
            byte[] buffer2 = new byte[] { 0x13, 0xA9, 0xDF, 0x41, };
            string aa2 = BitConverter.ToSingle(buffer2, 0).ToString("f2");
            UInt16 UPressure = 0;
            UPressure = (UInt16)(0xFF << 8) + 0xFA;

            int IntPressure = (Int16)UPressure;

            Console.WriteLine("准备开启程序");

            //开启客户端链接端口
            MyListener = new TcpListener(new IPEndPoint(IPAddress.Any, 5077));
            MyListener.Start();
            Console.WriteLine("开始监听5077端口");
            Thread listenThread = new Thread(new ThreadStart(ListenerThread.ListenClientConnect));
            listenThread.IsBackground = true;
            listenThread.Start();


            //开启连接超时的值守线程
            Console.WriteLine("开启连接超时的值守线程，单片机连接10分钟不通信会被移除，命令端连接10小时不通信会被移除");
            Thread CheckTimeOutThread = new Thread(new ThreadStart(ListenerThread.CheckConnctTimeOut));
            CheckTimeOutThread.IsBackground = true;
            CheckTimeOutThread.Start();



            ConsoleKeyInfo key;
            while (true)
            {
                key = Console.ReadKey(true);    //true参数是为了不显示在控制台上
                if (key.KeyChar == 'q' || key.KeyChar == 'Q')
                {
                    Console.WriteLine("read to exit!!!");
                    Thread.Sleep(1000);
                    break;

                }
                Thread.Sleep(1000);

            }


            //Console.WriteLine(CRC.ToModbusCRC16("0103020001"));
            //Console.ReadKey(true);    //true参数是为了不显示在控制台上
            
        }
    }
}
